<?php
namespace app\api\controller\vppz;


use app\common\library\token\driver\Redis;
use think\Cache;


use \app\admin\model\vppz\ServiceRecords as ServiceRecordsModel;

use \addons\vppz\library\Vpower;

class ServiceRecords extends App
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->ServiceRecordsModel = new ServiceRecordsModel;
    }


    //todo 提交预约
    public function createData(){
        $json = file_get_contents("php://input");
        // 将JSON字符串解码为PHP数组或对象
        $data = json_decode($json, true);
        $orderdata = array(
            'order_no'=>uniqid('yuyue_', true),
            'user_id'=>$this->_mine['id'],
            'status'=>$data['status'],
            'medical_history'=>$data['medicalHistory'],
            'attachments'=>json_encode($data['images']),
            'patient_name'=>$data['patientInfo']['name'],
            'patient_sex'=>$data['patientInfo']['gender'],
            'patient_age'=>$data['patientInfo']['age'],
            'contect_name'=>$data['contactInfo']['name'],
            'contect_phone'=>$data['contactInfo']['phone']
        );
        $ret=$this->ServiceRecordsModel->save($orderdata);
		$orderdata['id'] = $this->ServiceRecordsModel->id;
        $user = \app\admin\model\vppz\User::get($this->_mine['id']);
		if($ret){
            $content = '用户：'.$user['nickname']?$user['nickname']:'匿名用户'.'给病人'.$data['patientInfo']['name'].'提交诊疗建议。请及时处理';
            hook('send_message', [1, 'system', '诊疗建议', $content]);
            $redis = new Redis();
            $key = 'system_msg_key';
            $redis->lpush($key,1);
            $content1 = '您给病人'.$data['patientInfo']['name'].'提交的诊疗建议已提交完成。请及时关注官方回复动态';
            hook('send_message', [$this->_mine['id'], 'user', '诊疗建议', $content1]);
            \TempNotice::zhenliaowaitnoticetouser($this->_mine['openid'],$data['patientInfo'],array('content'=>$data['medicalHistory']));
            \TempNotice::zhenliaowaitnoticetoadmin($orderdata);
            $this->success('提交成功',$this->ServiceRecordsModel->id);
		}else{
            $this->error('抱歉，提交失败，重试看看呢');
        }
    }
    public function getAdviceData(){
        $id = input('id','');
        if(empty($id)){
            $this->success('',[]);
        }else{
            $res = $this->ServiceRecordsModel->find($id);
            $res['updatetime'] = datetime($res['updatetime'],'Y-m-d H:i:s');
            $this->success('成功',$res);
        }
    }
}
